package com.jh.storageview;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceViewHolder;

public class StorageFragment extends PreferenceFragmentCompat implements StoragePreference.StoragePreferenceLayoutLoadFinish, StorageViewObserver{

    public static final String TAG = "StorageFragment";

    public static final String STORAGE_SYS_G = "storage_sys_g";
    public static final String STORAGE_SYS_D = "storage_sys_d";
    public static final String STORAGE_DOC = "storage_doc";
    public static final String STORAGE_PKG = "storage_pkg";
    public static final String STORAGE_VIDEO = "storage_video";
    public static final String STORAGE_SOUND = "storage_sound";
    public static final String STORAGE_IMG = "storage_img";
    public static final String STORAGE_APP = "storage_app";

    public static final String[] keys = new String[]{
            STORAGE_SYS_G,
            STORAGE_SYS_D,
            STORAGE_DOC,
            STORAGE_PKG,
            STORAGE_VIDEO,
            STORAGE_SOUND,
            STORAGE_IMG,
            STORAGE_APP
    };

    StoragePreference storageSysG;
    StoragePreference storageSysD;
    StoragePreference storageDoc ;
    StoragePreference storagePkg ;
    StoragePreference storageVideo;
    StoragePreference storageSound;
    StoragePreference storageImg;
    StoragePreference storageApp ;

    @Override
    public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
        setPreferencesFromResource(R.xml.storage_fragment, rootKey);
        storageSysG = findPreference(STORAGE_SYS_G);
        storageSysD = findPreference(STORAGE_SYS_D);
        storageDoc = findPreference(STORAGE_DOC);
        storagePkg = findPreference(STORAGE_PKG);
        storageVideo = findPreference(STORAGE_VIDEO);
        storageSound = findPreference(STORAGE_SOUND);
        storageImg = findPreference(STORAGE_IMG);
        storageApp = findPreference(STORAGE_APP);
        if (storageSysG != null) storageSysG.setStoragePreferenceLayoutLoadFinish(this);
        if (storageSysD != null) storageSysD.setStoragePreferenceLayoutLoadFinish(this);
        if (storageDoc != null) storageDoc.setStoragePreferenceLayoutLoadFinish(this);
        if (storagePkg != null) storagePkg.setStoragePreferenceLayoutLoadFinish(this);
        if (storageVideo != null) storageVideo.setStoragePreferenceLayoutLoadFinish(this);
        if (storageSound != null) storageSound.setStoragePreferenceLayoutLoadFinish(this);
        if (storageImg != null) storageImg.setStoragePreferenceLayoutLoadFinish(this);
        if (storageApp != null) storageApp.setStoragePreferenceLayoutLoadFinish(this);

    }

    public void setSysGTitle(String title){
        storageSysG.setSTitle(title);
    }
    public void setSysDTitle(String title){
        storageSysD.setSTitle(title);
    }
    public void setDocTitle(String title){
        storageDoc.setSTitle(title);
    }
    public void setPkgTitle(String title){
        storagePkg.setSTitle(title);
    }
    public void setVideoTitle(String title){
        storageVideo.setSTitle(title);
    }
    public void setSoundTitle(String title){
        storageSound.setSTitle(title);
    }
    public void setImgTitle(String title){
        storageImg.setSTitle(title);
    }
    public void setAppTitle(String title){
        storageApp.setSTitle(title);
    }

    public void setSysGSummary(String summary){
        storageSysG.setSSummary(summary);
    }
    public void setSysDSummary(String summary){
        storageSysD.setSSummary(summary);
    }
    public void setDocSummary(String summary){
        storageDoc.setSSummary(summary);
    }
    public void setPkgSummary(String summary){
        storagePkg.setSSummary(summary);
    }
    public void setVideoSummary(String summary){
        storageVideo.setSSummary(summary);
    }
    public void setSoundSummary(String summary){
        storageSound.setSSummary(summary);
    }
    public void setImgSummary(String summary){
        storageImg.setSSummary(summary);
    }
    public void setAppSummary(String summary){
        storageApp.setSSummary(summary);
    }

    public StorageFragmentListenerPreferenceLoc mSFLoc;

    @Override
    public void layoutLoadFinish(int[] dy) {
        if (mSFLoc != null){
            mSFLoc.localCb(dy);
        }
    }

    @Override
    public void update(int index) {
        for (int i = 0; i < keys.length; i++) {
            StoragePreference preference = findPreference(keys[i]);
            if (preference == null){
                continue;
            }
            if (index == i){
                preference.removeBackgroundMask();
            }else{
                preference.setBackgroundMask();
            }
            if (index == -1){
                preference.removeBackgroundMask();
            }
        }
    }

    public interface StorageFragmentListenerPreferenceLoc{
        void localCb(int[] dy);
    }
    public void setSFLoc(StorageFragmentListenerPreferenceLoc listener){
        mSFLoc = listener;
    }
}
